package autoteam.ui.tabs;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JTabbedPane;
import javax.swing.JTable;

import autoteam.Child;
import autoteam.League;
import autoteam.Nights;
import autoteam.tables.TeamTableModel;
import autoteam.ui.AutoTeamTablePanel;
import autoteam.ui.dialogs.TeamCreator;
import autoteam.ui.dialogs.TeamEditor;
import db.DBRecord;
import db.DataAccessObject;

public class ManageTeamsTab extends TabPanel
{

	private static final long serialVersionUID = 461822798167944820L;

	private final JButton editButton = new JButton("Edit");
	private final JButton profileButton = new JButton("Profile");
	private final JButton addButton = new JButton("Add");

	private TeamTableModel teamModel;

	public ManageTeamsTab(JFrame parentFrame)
	{
		//testing: DO NOT USE
	}
	
	public ManageTeamsTab(League currentLeague)
	{
		super();
		this.setName("Teams Tab");

		// create the table
		JComboBox nightComboBox = new JComboBox(Nights.night);
		JComboBox coachBox = new JComboBox();
		JComboBox coachKidBox = new JComboBox();
		DataAccessObject dao = new DataAccessObject();
		List<DBRecord> coachRecords = dao.getAllRecords("coaches");
		for (DBRecord rec : coachRecords)
		{
			coachBox.addItem((String) rec.getFieldValue("firstname") + 
									  rec.getFieldValue("lastname"));
		}
		Vector children = currentLeague.getChildren();
		for (Object c : children)
		{
			coachKidBox.addItem(c);
		}
		
		TeamTableModel teamModel = new TeamTableModel(currentLeague,
				nightComboBox, null);
		final AutoTeamTablePanel theTablePanel = new AutoTeamTablePanel(
				teamModel);
		theTablePanel.setTableCellEditor(1, coachBox);
		theTablePanel.setTableCellEditor(2, coachKidBox);
		theTablePanel.setTableCellEditor(3, nightComboBox);

		this.add(theTablePanel);

	}

	public ManageTeamsTab(League currentLeague, JFrame parentFrame)
	{
		super();
		this.setName("Teams Tab");

		// create the table
		JComboBox nightComboBox = new JComboBox(Nights.night);

		teamModel = new TeamTableModel(currentLeague,
				nightComboBox, null);
		final AutoTeamTablePanel theTablePanel = new AutoTeamTablePanel(
				teamModel, editButton, addButton);

		theTablePanel.setTableCellEditor(3, nightComboBox);
		setButtonActions(parentFrame);
		this.add(theTablePanel);
	}

	
	protected void setButtonActions(final JFrame parentFrame)
	{
		// TODO Auto-generated method stub
		addButton.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				// TODO remove setCurrentFrame after integration is Complete
				// RSMSController.setCurrentFrame( new JFrame() );
				TeamCreator dialog = new TeamCreator(parentFrame,teamModel);
				dialog.setVisible(true);
			}
		});

		editButton.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				// TODO remove setCurrentFrame after integration is Complete
				// RSMSController.setCurrentFrame( new JFrame() );
				TeamEditor dialog = new TeamEditor(parentFrame);
				dialog.setVisible(true);
			}
		});

		profileButton.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				// TODO pop up the selected player's profile
			}
		});
	}

}
